<?php

class User extends Model {

    function verify_credentials($username, $password) {
        $query = "SELECT * FROM `users`
                  WHERE
                    `users`.`username` = '$username'
                    AND
                    `users`.`password` = '$password';";

        $result = $this->query($query, true);

        return $result;
    }

    function add($user) {
        $username = mysql_real_escape_string($user["username"]);
        $password = mysql_real_escape_string($user["password"]);
        $name = mysql_real_escape_string($user["name"]);
        $dob = mysql_real_escape_string($user["dob"]);
        $email = mysql_real_escape_string($user["email"]);
        $gender = mysql_real_escape_string($user["gender"]);
        $location = mysql_real_escape_string($user["location"]);
        $twitter_username = mysql_real_escape_string($user["twitter_username"]);
        $oauth_token = mysql_real_escape_string($user["oauth_token"]);
        $oauth_token_secret = mysql_real_escape_string($user["oauth_token_secret"]);

        $query = "INSERT INTO `users` (
                    `id`,
                    `role_id`,
                    `username`,
                    `password`,
                    `blocked`,
                    `name`,
                    `dob`,
                    `email`,
                    `gender`,
                    `location`,
                    `photo_id`,
                    `wall_rule`,
                    `twitter_username`,
                    `oauth_token`,
                    `oauth_token_secret`
                  )
                  VALUES (
                    NULL,
                    '0',
                    '$username',
                    '$password',
                    'false',
                    '$name',
                    '$dob',
                    '$email',
                    '$gender',
                    '$location',
                    '1',
                    '0',
                    '$twitter_username',
                    '$oauth_token',
                    '$oauth_token_secret'
                  );";
        
        $result = $this->query($query);

        return $result;
    }

    function follow($user_id, $target_user_id) {
        if (!$this->follows($user_id, $target_user_id)) {
            $query = "INSERT INTO `relations` (
                        `id`,
                        `user_id`,
                        `following_user_id`
                      )
                      VALUES (
                        NULL,
                        '$user_id',
                        '$target_user_id'
                      );";

            $result = $this->query($query);

            return $result;
        }
    }

    function unfollow($user_id, $target_user_id) {
        $query = "DELETE FROM `relations`
                  WHERE
                    `relations`.`user_id` = '$user_id'
                    AND
                    `relations`.`following_user_id` = '$target_user_id';";

        $result = $this->query($query);

        return $result;
    }

    function follows($user_id, $target_user_id) {
        $query = "SELECT * FROM `relations`
                  WHERE
                    `relations`.`user_id` = '$user_id'
                    AND
                    `relations`.`following_user_id` = '$target_user_id';";

        $result = $this->query($query);

        if (count($result) > 0) {
            return true;
        } else {
            return false;
        }
    }

    function add_wall($wall) {
        $author_user_id = $wall["author_user_id"];
        $recipient_user_id = $wall["recipient_user_id"];
        $content = $wall["content"];

        $query = "INSERT INTO `posts` (
                    `id`,
                    `author_user_id`,
                    `recipient_user_id`,
                    `timestamp`,
                    `content`
                  )
                  VALUES (
                    NULL,
                    '$author_user_id',
                    '$recipient_user_id',
                    CURRENT_TIMESTAMP,
                    '$content'
                  );";

        $result = $this->query($query);

        return $result;
    }

    function delete_wall($id) {
        $query = "DELETE FROM `posts`
                  WHERE
                    `posts`.`id` = '$id';";

        $result = $this->query($query);

        return $result;
    }

    function delete_wall_comment($id) {
        $query = "DELETE FROM `comments`
                  WHERE
                    `comments`.`id` = '$id';";

        $result = $this->query($query);

        return $result;
    }

    function add_wall_comment($comment) {
        $post_id = $comment["post_id"];
        $author_user_id = $comment["author_user_id"];
        $content = $comment["content"];

        $query = "INSERT INTO `comments` (
                    `id`,
                    `type`,
                    `post_id`,
                    `author_user_id`,
                    `timestamp`,
                    `content`
                  )
                  VALUES (
                    NULL,
                    '2',
                    '$post_id',
                    '$author_user_id',
                    CURRENT_TIMESTAMP,
                    '$content'
                );";

        $result = $this->query($query);

        return $result;
    }

    function get_wall($id, $offset = "0") {
        $query = "SELECT `posts`.*, `users`.`name` FROM
                    `posts` JOIN `users`
                  WHERE
                    `posts`.`author_user_id` = `users`.`id`
                    AND
                    `posts`.`recipient_user_id` = '$id'
                  ORDER BY
                    `posts`.`id` DESC
                  LIMIT $offset , 5;";

        $walls = $this->query($query);

        foreach ($walls as &$wall) {
            $post_id = $wall["Post"]["id"];

            $query = "SELECT `comments`.*, `users`.`name` FROM
                        `comments` JOIN `users`
                      WHERE
                        `comments`.`author_user_id` = `users`.`id`
                        AND
                        `comments`.`post_id` = '$post_id'
                        AND
                        `comments`.`type` = 2;";

            $comments = $this->query($query);

            $wall["Comments"] = $comments;
        }

        return $walls;
    }

    function update($user) {
        $user_id = $user["id"];
        $password = mysql_real_escape_string($user["password"]);
        $name = mysql_real_escape_string($user["name"]);
        $dob = mysql_real_escape_string($user["dob"]);
        $email = mysql_real_escape_string($user["email"]);
        $gender = mysql_real_escape_string($user["gender"]);
        $location = mysql_real_escape_string($user["location"]);
        $blocked = $user["blocked"];
        $wall_rule = $user["wall_rule"];
        $twitter_username = mysql_real_escape_string($user["twitter_username"]);
        $oauth_token = mysql_real_escape_string($user["oauth_token"]);
        $oauth_token_secret = mysql_real_escape_string($user["oauth_token_secret"]);

        $query = "UPDATE `users`
                  SET
                    `password` = '$password',
                    `name` = '$name',
                    `dob` = '$dob',
                    `email` = '$email',
                    `gender` = '$gender',
                    `location` = '$location',
                    `blocked` = '$blocked',
                    `wall_rule` = '$wall_rule',
                    `twitter_username` = '$twitter_username',
                    `oauth_token` = '$oauth_token',
                    `oauth_token_secret` = '$oauth_token_secret'
                  WHERE
                    `id` = $user_id;";

        $result = $this->query($query);

        return $result;
    }

    function followers($id) {
        $query = "SELECT `relations`.`user_id` FROM `relations`
                  WHERE
                    `relations`.`following_user_id` = '$id';";

        $result = $this->query($query);

        $followers = array();

        foreach ($result as $follower) {
            $followers[] = $follower["Relation"]["user_id"];
        }

        return $followers;
    }

    function followings($id) {
        $query = "SELECT `relations`.`following_user_id` FROM `relations`
                  WHERE
                    `relations`.`user_id` = '$id';";

        $result = $this->query($query);

        $followers = array();

        foreach ($result as $follower) {
            $followers[] = $follower["Relation"]["user_id"];
        }

        return $followers;
    }

    function set_photo($user_id, $photo_id) {
        $query = "UPDATE `users`
                  SET
                    `photo_id` = '$photo_id'
                  WHERE
                    `id` = $user_id;";

        $result = $this->query($query);

        return $result;
    }

    function followers_complete($id) {
        $query = "SELECT `users`.`id`, `photos`.`filename`
                  FROM `relations` JOIN `users` JOIN `photos`
                  WHERE
                    `relations`.`following_user_id` = $id
                  AND
                    `relations`.`user_id` = `users`.`id`
                  AND
                    `photos`.`id` = `users`.`photo_id`";

        $result = $this->query($query);

        return $result;
    }

    function followings_complete($id) {
        $query = "SELECT `users`.`id`, `photos`.`filename`
                  FROM `relations` JOIN `users` JOIN `photos`
                  WHERE
                    `relations`.`user_id` = $id
                  AND
                    `relations`.`following_user_id` = `users`.`id`
                  AND
                    `photos`.`id` = `users`.`photo_id`";

        $result = $this->query($query);

        return $result;
    }

    function notification($id) {
        $query = "SELECT * FROM `notifications`
                  WHERE
                    `author_user_id` = $id";

        $result = $this->query($query);
        return $result;
    }

    function notification_delete($id) {
        $query = "DELETE FROM `notifications`
                  WHERE
                    `author_user_id` = $id";

        $result = $this->query($query);
    }

    function add_notification($author_user_id, $user_id) {
        $query = "SELECT * FROM `users`
                  WHERE
                    `id` = $author_user_id";

        $result = $this->query($query,true);
        $name = $result['User']['name'];
        $query = "INSERT INTO `notifications` (
                    `author_user_id`,
                    `type`,
                    `user_id`,
                    `user_name`
                  )
                  VALUES (
                    $user_id,
                    1,
                    $author_user_id,
                    '$name'
                )";

        $result = $this->query($query);
    }

    function set_login($id) {
        $query = "UPDATE `online`
                  SET
                    `status` = 1
                  WHERE
                    `user_id` = $id;";

        $result = $this->query($query);
    }

    function set_logout($id) {
        $query = "UPDATE `online`
                  SET
                    `status` = 0
                  WHERE
                    `user_id` = $id;";

        $result = $this->query($query);
    }

    function add_status($id) {
        $query = "INSERT INTO `online` (
                    `user_id`
                  )
                  VALUES (
                    $id
                )";

        $result = $this->query($query);
    }

    function chat_ajax($id) {
        //follower
        $query = "SELECT `users`.`id`, `photos`.`filename`, `users`.`name`
                  FROM `relations` JOIN `users` JOIN `photos`
                  WHERE
                    `relations`.`following_user_id` = $id
                  AND
                    `relations`.`user_id` = `users`.`id`
                  AND
                    `photos`.`id` = `users`.`photo_id`";

        $follower = $this->query($query);

        $result["follower"] = array();
        foreach($follower as $user):
            $user_id = $user['User']['id'];
            $query = "SELECT * FROM `online` WHERE `user_id` = $user_id";
            $status = $this->query($query,true);
            if ($status['Online']['status'] == 1) {
                $result["follower"][] = $user;
            }
        endforeach;

        //following
        $query = "SELECT `users`.`id`, `photos`.`filename`, `users`.`name`
                  FROM `relations` JOIN `users` JOIN `photos`
                  WHERE
                    `relations`.`user_id` = $id
                  AND
                    `relations`.`following_user_id` = `users`.`id`
                  AND
                    `photos`.`id` = `users`.`photo_id`";

        $following = $this->query($query);

        $result["following"] = array();
        foreach($following as $user):
            $user_id = $user['User']['id'];
            $query = "SELECT * FROM `online` WHERE `user_id` = $user_id";
            $status = $this->query($query,true);
            if ($status['Online']['status'] == 1) {
                $result["following"][] = $user;
            }
        endforeach;

        return $result;
    }

    function get_user($id) {
        $query = "SELECT * FROM `users`
                  WHERE
                    `id` = $id";

        $result = $this->query($query, true);
        return $result;
    }

    function submit_chat($from, $to, $content) {
        $query = "INSERT INTO `chats` (
                    `from`,
                    `to`,
                    `content`
                  )
                  VALUES (
                    $from,
                    $to,
                    '$content'
                )";

        $result = $this->query($query);
    }

    function get_chat($id, $recipient) {
        $query = "SELECT * FROM `chats`
                  WHERE
                    `from` = $id
                  OR
                    `to` = $id";

        $result["chats"] = $this->query($query);

        $query = "SELECT * FROM `users`
                  WHERE
                    `id` = $recipient";

        $result["recipients"] = $this->query($query, true);
        return $result;
    }

    function get_online($user_id) {
        $query = "SELECT * FROM `online` WHERE `user_id` = $user_id";
        $result = $this->query($query,true);
        return $result;
    }
}
